// pages/article/list.js

const App = getApp();



Page({

  /**
   * 页面的初始数据
   */
  data: {
    articleList: [],
    page: 1,
    page_size: 10,
    is_no_data: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options)
    var _this = this;

    if (options.topic_id){
      _this.setData({
        'topic_id': options.topic_id
      })
    }
    if (options.channel_id) {
      _this.setData({
        'channel_id': options.channel_id
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    _this.setData({
      page: 1,
      articleList: [],
      is_no_data: false
    })

    if (_this.data.topic_id){
      _this.getArticleList({
        topic_id: _this.data.topic_id
      });
    }
   
    if (_this.data.channel_id) {
      _this.getArticleList({
        channel_id: _this.data.channel_id
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var _this = this;

    if (_this.data.is_no_data) {
      console.log('true');
      return;
    }
    if (_this.data.topic_id){
      this.getArticleList({
        topic_id: _this.data.topic_id
      });
    }
    
    if (_this.data.channel_id) {
      this.getArticleList({
        channel_id: _this.data.channel_id
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getArticleList(data) {
    var _this = this;
   
    var _data = Object.assign({}, data, {
      page: _this.data.page,
      page_size: _this.data.page_size
    });

    
    wx.showLoading({title:'loading~'});
    App.Api.post('/iNews/getNewsDataPage', _data)
      .then((data) => {
        wx.hideLoading();
        var _articleList = data;

        _this.setData({
          articleList: _this.data.articleList.concat(_articleList)
        });
        
        if (_articleList.length < _this.data.page_size) {
          _this.setData({
            is_no_data: true
          })
          return;
        }
        if (_articleList.length >= _this.data.page_size) {
          _this.setData({
            page: _this.data.page + 1
          })
          return;
        }


      })
      .catch((error) => {
        console.log('error', error);
        wx.hideLoading();
        App.topTips.showTopTips.call(_this, error.msg);
      })
  }
})